package com.ucode.wap.cgms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ucode.cgms.service.CgmsGiftRewardOrderService;
import com.ucode.cgms.service.CgmsGiftService;
import com.ucode.member.service.MemberService;
import com.ucode.model.cgms.CgmsGift;
import com.ucode.model.cgms.CgmsGiftRewardOrder;
import com.ucode.model.cgms.convert.CgmsGiftRewardOrderConvert;
import com.ucode.model.cgms.enums.CgmsPayStateEnum;
import com.ucode.model.cgms.form.CgmsGiftRewardOrderForm;
import com.ucode.model.cgms.vo.CgmsGiftRewardOrderVO;
import com.ucode.model.member.Member;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import com.ucode.wap.cgms.service.ApiCgmsGiftRewardOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Tag(name = "移动端 - 陪玩社区系统 - 礼物打赏订单")
@RestController
@RequestMapping("/cgms/gift-reward-order")
@Validated
public class ApiCgmsGiftRewardOrderController {

    private static Log log = LogFactory.get();
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;
    @Resource
    private CgmsGiftRewardOrderService cgmsGiftRewardOrderService;
    @Resource
    ApiCgmsGiftRewardOrderService apiCgmsGiftRewardOrderService;
    @Resource
    private MemberService memberService;
    @Resource
    private CgmsGiftService cgmsGiftService;

    @NeedLogin
    @Operation(summary = "我的打赏分页列表")
    @GetMapping(value = "/my-reward/page-list")
    public ResponseResult<Paginator<CgmsGiftRewardOrderVO>> pageListByMyComment(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("rewardUserId", currentLoginUserHandler.getCurrentUserId());
        condition.put("payState", CgmsPayStateEnum.SUCCESS.getState());
        condition.put("deltag", false);
        Paginator<CgmsGiftRewardOrder> paginator = cgmsGiftRewardOrderService.queryPageForList(condition, pageSize, pageNum);
        Paginator<CgmsGiftRewardOrderVO> paginatorVo = CgmsGiftRewardOrderConvert.INSTANCE.convertPage(paginator);
        if(CollUtil.isNotEmpty(paginatorVo.getList())){
            Set<Long> userIds = new HashSet<>();
            Set<Long> giftIds = new HashSet<>();
            for(CgmsGiftRewardOrderVO vo : paginatorVo.getList()){
                userIds.add(vo.getCompanionUserId());
                if(ObjUtil.isNotNull(vo.getGiftId()) && !ObjUtil.equals(vo.getGiftId(),0L)){
                    giftIds.add(vo.getGiftId());
                }
            }

            Map<Long, Member>  memberMap = memberService.getMap(userIds);
            Map<Long, CgmsGift> giftMap = cgmsGiftService.getMap(giftIds);

            for(CgmsGiftRewardOrderVO vo : paginatorVo.getList()){
                Member member = memberMap.get(vo.getCompanionUserId());
                if(ObjectUtil.isNotNull(member)){
                    vo.setCompanionUserNickname(member.getNickname());
                    vo.setCompanionUserAvatar(member.getAvatar());
                }

                vo.setGift(giftMap.get(vo.getGiftId()));
            }
        }

        return ResponseResult.success(paginatorVo);
    }

    @NeedLogin
    @Operation(summary = "打赏我的分页列表")
    @GetMapping(value = "/reward-me/page-list")
    public ResponseResult<Paginator<CgmsGiftRewardOrderVO>> pageListByCommentMe(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        condition.put("companionUserId", currentLoginUserHandler.getCurrentUserId());
        condition.put("payState", CgmsPayStateEnum.SUCCESS.getState());
        condition.put("deltag", false);
        Paginator<CgmsGiftRewardOrder> paginator = cgmsGiftRewardOrderService.queryPageForList(condition, pageSize, pageNum);
        Paginator<CgmsGiftRewardOrderVO> paginatorVo = CgmsGiftRewardOrderConvert.INSTANCE.convertPage(paginator);
        if(CollUtil.isNotEmpty(paginatorVo.getList())){

            Set<Long> userIds = new HashSet<>();
            Set<Long> giftIds = new HashSet<>();
            for(CgmsGiftRewardOrderVO vo : paginatorVo.getList()){
                userIds.add(vo.getRewardUserId());
                if(ObjUtil.isNotNull(vo.getGiftId()) && !ObjUtil.equals(vo.getGiftId(),0L)){
                    giftIds.add(vo.getGiftId());
                }
            }

            Map<Long, Member>  memberMap = memberService.getMap(userIds);
            Map<Long, CgmsGift> giftMap = cgmsGiftService.getMap(giftIds);

            for(CgmsGiftRewardOrderVO vo : paginatorVo.getList()){
                Member member = memberMap.get(vo.getRewardUserId());
                if(ObjectUtil.isNotNull(member) && !vo.getAnonymityState()){
                    vo.setRewardUserNickname(member.getNickname());
                    vo.setRewardUserAvatar(member.getAvatar());
                }

                vo.setGift(giftMap.get(vo.getGiftId()));
            }
        }

        return ResponseResult.success(paginatorVo);
    }

    /**
     * 创建礼物打赏订单记录
     * @param form
     * @return
     */
    @NeedLogin
    @PostMapping("create")
    @Operation(summary = "创建礼物打赏订单记录")
    public ResponseResult<Long> createCgmsGiftRewardOrder(@Valid @RequestBody CgmsGiftRewardOrderForm form) {
        Long payOrderId = apiCgmsGiftRewardOrderService.submitOrder(form);
        return payOrderId != null ? ResponseResult.success(payOrderId) : ResponseResult.failed();
    }

}
